Some objects such as lists are iterable and thus a for loop can be used to iterate over all items or a set of items based on a condition to peforms a fucntion or operation on each items in the list for example.)


In [2]:
my_iterable = [1,2,3]
for my_iterable in my_iterable:
    print(my_iterable)


1
2
3

In [23]:
x=[1,2,3]

y= [str(a) + b + c for a in x for b in ['a','b','c'] for c in ['x','y','z']]

In [24]:
y


Out[24]:
['1ax',
 '1ay',
 '1az',
 '1bx',
 '1by',
 '1bz',
 '1cx',
 '1cy',
 '1cz',
 '2ax',
 '2ay',
 '2az',
 '2bx',
 '2by',
 '2bz',
 '2cx',
 '2cy',
 '2cz',
 '3ax',
 '3ay',
 '3az',
 '3bx',
 '3by',
 '3bz',
 '3cx',
 '3cy',
 '3cz']

In [26]:
from itertools import combinations, product

symbols = "abcdefghijklmnopqrstuvwxyz"
max_length = 2

class SiteSuffix:

    # generator of all combinations
    def words1(chars=symbols, max_len=max_length):
        for length in xrange(1, max_length + 1):
            for word in map(''.join, combinations(symbols, length)):
                yield word

    # generator of all combinations allowing repetitions
    def words1(chars=symbols, max_len=max_length):
     
         for length in xrange(1, max_length + 1):
            for word in map(''.join, product(*[symbols]*length)):
                yield word

    chars = []

    for word in words1():
        #do something with word
        chars.append(word)

    c = chars[26::1]    
    print(str(c) + '\nFinished printing country code appendices, with love from rusha.')
    c # should just quietly return this value ;)


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-26-88e656ace10d> in <module>()
      4 max_length = 2
      5 
----> 6 class SiteSuffix:
      7 
      8     # generator of all combinations

<ipython-input-26-88e656ace10d> in SiteSuffix()
     21     chars = []
     22 
---> 23     for word in words1():
     24         #do something with word
     25         chars.append(word)

<ipython-input-26-88e656ace10d> in words1(chars, max_len)
     15     def words1(chars=symbols, max_len=max_length):
     16 
---> 17          for length in xrange(1, max_length + 1):
     18             for word in map(''.join, product(*[symbols]*length)):
     19                 yield word

NameError: name 'xrange' is not defined

In [66]:
from itertools import combinations, product

symbols = "abcdefghijklmnopqrstuvwxyz"
max_length = 2

class SiteSuffix:

    # generator of all combinations
    def words(chars=symbols, max_len=max_length):
        for length in xrange(1, max_length + 1):
            for word in map(''.join, combinations(symbols, length)):
                yield word

    # generator of all combinations allowing repetitions
    def words(chars=symbols, max_len=max_length):
         for length in xrange(1, max_length + 1):
            for word in map(''.join, product(*[symbols]*length)):
                yield word
    chars = []

    for word in words():
        #do something with word
        chars.append(word)

c = chars[26::1]
    
print(str(c) + '\nFinished printing country code appendices, with love from rusha.')


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-66-c14be1c92522> in <module>()
      4 max_length = 2
      5 
----> 6 class SiteSuffix:
      7 
      8     # generator of all combinations

<ipython-input-66-c14be1c92522> in SiteSuffix()
     19     chars = []
     20 
---> 21     for word in words():
     22         #do something with word
     23         chars.append(word)

<ipython-input-66-c14be1c92522> in words(chars, max_len)
     14     # generator of all combinations allowing repetitions
     15     def words(chars=symbols, max_len=max_length):
---> 16          for length in xrange(1, max_length + 1):
     17             for word in map(''.join, product(*[symbols]*length)):
     18                 yield word

NameError: name 'xrange' is not defined

In [64]:
c


Out[64]:
[]

In [56]:



[]
Finished printing country code appendices, with love from rusha.

In [55]:



[]
Finished printing country code appendices, with love from rusha.

In [ ]: